// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  if (event.action == 'add') {
    return await cloud.database().collection('doctorInfo').add({
      data:{
        doctorId:event.doctorId,
        doctorName:event.doctorName,
        doctorPosition:event.doctorPosition,
        doctorExpertise:event.doctorExpertise,
        doctorStartTime:event.doctorStartTime,
        doctorEndTime:event.doctorEndTime,
        doctorPay:event.doctorPay,
        doctorMaxPatient:event.doctorMaxPatient,
        doctorPic:event.doctorPic,
        userDoctorAdmin:event.userDoctorAdmin,
        userDoctorName:event.userDoctorName,
        userDoctorPwd:event.userDoctorPwd,
        nowNumber:0,
        lastNumber:0,
      }
    }).then(res => {
      return res
    }).catch(res => {
      return res
    })
  } 
  else if(event.action=="del"){
    return await cloud.database().collection('doctorInfo').where({
      userDoctorAdmin:event.userDoctorAdmin
    }).remove()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="change"){
    return await cloud.database().collection('doctorInfo').where({
      userDoctorAdmin:event.userDoctorAdmin
    }).update({
      data:{
        doctorId:event.doctorId,
        doctorName:event.doctorName,
        doctorPosition:event.doctorPosition,
        doctorExpertise:event.doctorExpertise,
        doctorStartTime:event.doctorStartTime,
        doctorEndTime:event.doctorEndTime,
        doctorPay:event.doctorPay,
        doctorMaxPatient:event.doctorMaxPatient,
        doctorPic:event.doctorPic
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="changeNowNumber"){
    return await cloud.database().collection('doctorInfo').where({
      doctorId:event.doctorId
    }).update({
      data:{
        nowNumber:event.nowNumber,
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="changeLastNumber"){
    return await cloud.database().collection('doctorInfo').where({
      doctorId:event.doctorId
    }).update({
      data:{
        lastNumber:event.lastNumber,
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="find"){
    return await cloud.database().collection('doctorInfo').get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findOne"){
    return await cloud.database().collection('doctorInfo').where({
      userDoctorAdmin:event.userDoctorAdmin
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findOneId"){
    return await cloud.database().collection('doctorInfo').where({
      doctorId:event.doctorId
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
}

